package org.dei.perla.sys.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import org.dei.perla.sys.core.PerlaRuntime;

public class PerlaSQLExec {

	private static Connection dbConnection;
	private static Connection internalDBConnection;
	
	public static void init() throws SQLException{
		PerlaRuntime runtime = PerlaRuntime.getDefaultRuntime();
		String dbname = runtime.getProperty("PERLA_DB");
		String internalDBName = runtime.getProperty("PERLA_INTERNAL_DB");
		String dbauth = runtime.getProperty("PERLA_DB_AUTH");

		if(dbname == null)
			dbname = "jdbc:mysql://localhost/perla?";

		if(dbauth == null)
			dbauth = "user=perla&password=perla";
		
		if(internalDBName == null)
			internalDBName = "jdbc:mysql://localhost/perla_internal?";
		
		dbConnection = DriverManager.getConnection(dbname + dbauth);
		internalDBConnection = DriverManager.getConnection(internalDBName + dbauth);
		
	}
	
	public static ResultSet exec(PreparedStatement preparedStatement) throws SQLException {
		ResultSet resultSet = preparedStatement.executeQuery();
		return resultSet;
	}
	
	public static ResultSet exec(String string) throws SQLException {
		PreparedStatement preparedStatement = dbConnection.prepareStatement(string);
		ResultSet resultSet = preparedStatement.executeQuery();
		return resultSet;
	}

	public static ResultSet execInternal(PreparedStatement preparedStatement) throws SQLException {
		ResultSet resultSet = preparedStatement.executeQuery();
		return resultSet;
	}
	
	public static ResultSet execInternal(String string) throws SQLException {
		PreparedStatement preparedStatement = internalDBConnection.prepareStatement(string);
		ResultSet resultSet = preparedStatement.executeQuery();
		return resultSet;
	}
	
	public static <T> void fetchColumnValues(int columnindex, PreparedStatement statement, ArrayList<T> arrayList) throws SQLException{
		if(statement.execute()){
			fetchColumnValues(columnindex, statement.getResultSet(), arrayList);
		}
		else{
			throw new SQLException();
		}
	}
	
	public static <T> void fetchColumnValues(String columnName, PreparedStatement statement,  ArrayList<T> arrayList) throws SQLException{
		if(statement.execute()){
			fetchColumnValues(columnName, statement.getResultSet(), arrayList);
		}
		else{
			throw new SQLException();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static <T> void fetchColumnValues(int columnindex, ResultSet set, ArrayList<T> arrayList) throws SQLException{
		while(set.next()){
			T object = (T) set.getObject(columnindex);
			arrayList.add(object);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static <T> void fetchColumnValues(String columnName, ResultSet set, ArrayList<T> arrayList) throws SQLException{
		while(set.next()){
			T object = (T) set.getObject(columnName);
			arrayList.add(object);
		}
	}
	
	public static PreparedStatement createPreparedStatement(String query) throws SQLException {
		PreparedStatement statement = dbConnection.prepareStatement(query);
		return statement;
	}
	
	public static PreparedStatement createPreparedStatementInternal(String query) throws SQLException {
		PreparedStatement statement = internalDBConnection.prepareStatement(query);
		return statement;
	}

}
